package bean.back.adminlist;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BackAdminlistBean {
    int code;
    String message;
    int total_results;
    List<BackAdminListItem> list;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getTotal_results() {
        return total_results;
    }

    public void setTotal_results(int total_results) {
        this.total_results = total_results;
    }

    public List<BackAdminListItem> getList() {
        return list;
    }

    public void setList(List<BackAdminListItem> list) {
        this.list = list;
    }

    public String query(String token, String id){
        JDBCConnection jdbc = new JDBCConnection();
        String tokenArry[] = token.split("#");
        try {
            if(checkPermissions(tokenArry[0],tokenArry[2],jdbc)){
                String querySQL = "select adminname,admintel from lotmanager where lotid = '"+id+"'";
                ResultSet rs = jdbc.executeQuery(querySQL);
                total_results=0;
                list = new ArrayList<>();
                while (rs.next()){
                    total_results++;
                    BackAdminListItem item = new BackAdminListItem();
                    item.setName(rs.getString("adminname"));
                    item.setTel(rs.getString("admintel"));
                    list.add(item);
                }
                code = 0;
                message = "成功";
            }
            else {
                code = 200;
                message = "token错误或者用户无权限";
            }
        }catch (Exception e){
            code = 100;
            message = "token错误";
        }
        return JSON.toJSONString(this);
    }

    private boolean checkPermissions(String operator_id, String operator_pwd, JDBCConnection jdbc){
        String querySQL = "select backpsd from backstage where backaccount = '"+operator_id+"'";
        ResultSet rs = jdbc.executeQuery(querySQL);
        try {
            rs.next();
            if(operator_pwd.equals(rs.getString("backpsd"))) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
